<?php
namespace app\admin\controller;
use think\Db;
class Userhistory extends Base
{

    public function lists(){
        if( is_post() ){
            $page    = input('page');
            $keyword = input('keyword');
            $i_age   = input('i_age');
            $b_type  = input('b_type');
            $p_yy    = input('p_yy');
            $froms   = input('froms');
            
            #sql
            $sql = "
                select
                    [1]
                from
                    user_history a
                left join
                    froms b
                on
                    a.froms = b.id
                where 
                    1 = ?
                ";
            $param = [1];
            if ( $i_age != '100all' ) {
                $temp = explode("-", $i_age);
                $sql .= " and (a.i_age < ? and a.i_age > ?)";
                $param[] = $temp[1];
                $param[] = $temp[0];
            }
            if ( $b_type != '100all' ) {
                $sql .= " and a.b_type=?";
                $param[] = $b_type;
            }
            if ( $p_yy != '100all' ) {
                $sql .= " and a.p_yy=?";
                $param[] = $p_yy;
            }
            if ( $froms != '100all' ) {
                $sql .= " and a.froms = ?";
                $param[] = $froms;
            }
            if ( !empty($keyword) ) {
                $sql .= " and (a.name like ? or a.i_pro like ? or a.p_pro like ?)";
                $param[] = "%{$keyword}%";
                $param[] = "%{$keyword}%";
                $param[] = "%{$keyword}%";
            }
            
            #分页
            $sql_count = str_replace("[1]", "count(*) as total", $sql);
            $total     = Db::query($sql_count, $param);
            $total     = $total[0]['total'];
            $page_size   = c('page_size');
            $page        = com_cur_page($total, $page,  $page_size);
			$page_data   = com_page_data($total, $page,  $page_size);
            
            //列表
			$start       = ($page - 1) * $page_size;
			$sql        .= " order by a.id desc limit $start, $page_size";
			$field       = "a.*,b.title";
			$sql_list    = str_replace("[1]", $field, $sql);
            $list        = Db::query($sql_list, $param);
            
            //格式数据
            foreach($list as $k => $v){           
                $list[$k]['create_time'] = com_format_time($v['create_time']);
                $list[$k]['froms'] = $v['title'];
            }
			$com_body    = com_build_table($list, array("id","name","phone","email","idcard","bank_no","froms","i_age","p_pro","i_pro", "create_time"));
			$com_page    = com_build_page( $page_data );
            $config = [
                'data'   => [
                    "com_body"  => $com_body,
                    "com_page"  => $com_page,
                ],
                'suc'    => [
                    'js_action' => 'function.iphone',
                ],
            ];
			com_check($config);
        }
        #赋值
        $froms = [];
        $rs = Db::name('froms')->select();
        foreach ($rs as $v){
            $froms[$v['id']] = $v['title'];
        }
        $data = [
            'nav'       => ['数据管理', '列表'],
            'i_age'     => ['100all'=>'年龄段'] + c('type.i_age'),
            'b_type'    => ['100all'=>'卡类型'] + c('type.b_type'),
            'p_yy'      => ['100all'=>'运营商'] + c('type.p_yy'),
            'froms'     => ['100all'=>'来源']  + $froms,
        ];
        return view('', $data);
    }
    
}